/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Form;

import BLL.BLLManu;
import DTO.DTOManufacture;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Pham
 */
public class JPanelManageManufacture extends javax.swing.JPanel {
    BLLManu bllManu;
    /**
     * Creates new form JPanelManageManufacture
     */
    public JPanelManageManufacture() {
        bllManu = new BLLManu();
        initComponents();
        DefaultTableModel tbModel = new DefaultTableModel();
        String[] cols = {"Mã hãng SX", "Tên hãng SX"};
        tbModel.setColumnIdentifiers(cols);
        tbManu.setModel(tbModel);
        loadData();
    }
    
    
    private void loadData()
    {
        DefaultTableModel tbModel = (DefaultTableModel)tbManu.getModel();
        for(int i = tbModel.getRowCount() - 1; i >= 0; i--){
            tbModel.removeRow(i);
        }
        
        for(DTOManufacture itemManu : bllManu.getmanufactures())
        {
            tbModel.addRow(new Object[]{itemManu.getManuID(), itemManu.getManuName()});
        }
        
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {//GEN-BEGIN:initComponents

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtManuName = new javax.swing.JTextField();
        lbIdManu = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbManu = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        btnNew = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        btnDel = new javax.swing.JButton();

        setRequestFocusEnabled(false);
        setVerifyInputWhenFocusTarget(false);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Tùy chỉnh"));
        jPanel2.setPreferredSize(new java.awt.Dimension(424, 416));
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel2.setText("Mã hãng sản xuất");
        jPanel2.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 32, -1, -1));

        jLabel3.setText("Tên hãng");
        jPanel2.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 60, -1, -1));
        jPanel2.add(txtManuName, new org.netbeans.lib.awtextra.AbsoluteConstraints(129, 57, 190, -1));

        lbIdManu.setText("id");
        jPanel2.add(lbIdManu, new org.netbeans.lib.awtextra.AbsoluteConstraints(129, 32, -1, -1));

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Danh sách hãng sản xuất", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 51, 0)));
        jPanel3.setPreferredSize(new java.awt.Dimension(432, 422));

        tbManu.setAutoCreateRowSorter(true);
        tbManu.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbManu.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbManuMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(tbManu);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
                .addContainerGap())
        );

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(102, 0, 0));
        jLabel1.setText("Hãng sản xuất");

        jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btnNew.setBackground(new java.awt.Color(51, 102, 0));
        btnNew.setForeground(new java.awt.Color(255, 255, 255));
        btnNew.setText("Thêm mới");
        btnNew.setPreferredSize(new java.awt.Dimension(90, 30));
        btnNew.setSelected(true);
        btnNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewActionPerformed(evt);
            }
        });
        jPanel4.add(btnNew);

        btnSave.setBackground(new java.awt.Color(0, 102, 51));
        btnSave.setForeground(new java.awt.Color(255, 255, 255));
        btnSave.setText("Lưu");
        btnSave.setPreferredSize(new java.awt.Dimension(90, 30));
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        jPanel4.add(btnSave);

        btnDel.setBackground(new java.awt.Color(153, 153, 153));
        btnDel.setText("Xóa");
        btnDel.setPreferredSize(new java.awt.Dimension(90, 30));
        btnDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDelActionPerformed(evt);
            }
        });
        jPanel4.add(btnDel);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 361, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(61, 61, 61))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(330, 330, 330)
                .addComponent(jLabel1)
                .addContainerGap(398, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(132, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }//GEN-END:initComponents

    private void tbManuMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbManuMousePressed
        try{
            int rowSelect = tbManu.getSelectedRow();
            if(rowSelect > -1)
            {
                int id = (int)tbManu.getValueAt(tbManu.getSelectedRow(), 0);
                DTOManufacture dtoManu = bllManu.getManuByID(id);
                lbIdManu.setText(String.valueOf(dtoManu.getManuID()));
                txtManuName.setText(dtoManu.getManuName());
            }
            
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null, "Error");
        }
    }//GEN-LAST:event_tbManuMousePressed

    private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewActionPerformed
        lbIdManu.setText("");
        txtManuName.setText("");
    }//GEN-LAST:event_btnNewActionPerformed

    //method action add new 
    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        try
        {
            if("".equals(txtManuName.getText().trim())){
                JOptionPane.showMessageDialog(null, "Vui lòng nhập tên hãng sản xuất");
            }
            else{
                String manuName = txtManuName.getText().trim();
                DTOManufacture dtoManu = new DTOManufacture();
                dtoManu.setManuName(manuName);
                if(bllManu.insertManu(dtoManu)){
                    JOptionPane.showMessageDialog(null, "Đã thêm");
                    loadData();
                }
                    
                else
                    JOptionPane.showMessageDialog(null, "Không thể thêm");
            }
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null, "Error");
        }
    }//GEN-LAST:event_btnSaveActionPerformed

    //method action delete a manufacture
    private void btnDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDelActionPerformed
        try{
            int rowSelect = tbManu.getSelectedRow();
            if(rowSelect > -1)
            {
                int checkSure = JOptionPane.showConfirmDialog(null, "Bạn có chắc muốn xóa", "Thông báo", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                if(checkSure == JOptionPane.YES_OPTION){
                    DefaultTableModel tbModel = (DefaultTableModel)tbManu.getModel();
                    int[] rows = tbManu.getSelectedRows();
                    for(int i = rows.length - 1; i >= 0; i--)
                    {
                        int id = (int)tbModel.getValueAt(rows[i], 0);
                        tbModel.removeRow(rows[i]);
                        bllManu.deleteManu(id);
                    }
                    lbIdManu.setText("");
                    txtManuName.setText("");
                }
                
            }
            else
                JOptionPane.showMessageDialog(null, "Chọn hãng SX muốn xóa");
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null, "Error");
        }
    }//GEN-LAST:event_btnDelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDel;
    private javax.swing.JButton btnNew;
    private javax.swing.JButton btnSave;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbIdManu;
    private javax.swing.JTable tbManu;
    private javax.swing.JTextField txtManuName;
    // End of variables declaration//GEN-END:variables
}
